﻿using System.Text;
using System.Collections;
namespace System.Web.Hosting
{
    public partial class WebHostWorkerRequest
    {
        protected string _appPath;
        protected string _appPathTranslated;
        protected int _contentType;
        protected int _contentAvailLength;
        protected int _queryStringLength;
        protected string _filePath;
        protected string _method;
        protected string _path;
        protected string _pathInfo;
        protected string _pathTranslated;

        private string[] _knownRequestHeaders;
        private static readonly char[] _colonOrNL = new char[] { ':', '\n' };

        internal int GetAdditionalPostedContentCore(byte[] bytes, int offset, int bufferSize)
        {
            return 0;
        }

        public override string GetAppPath()
        {
            return _appPath;
        }

        public override string GetAppPathTranslated()
        {
            return _appPathTranslated;
        }

        internal int GetBasicsCore(byte[] buffer, int size, int[] contentInfo)
        {
            return 0;
        }

        internal void Initialize()
        {
            ReadRequestBasics();
            if (((_appPathTranslated != null) && (_appPathTranslated.Length > 2)) && !_appPathTranslated.EndsWith("\\"))
                _appPathTranslated = _appPathTranslated + @"\";
        }

        private string[] ReadBasics(int[] contentInfo)
        {
            RecyclableByteBuffer buffer = new RecyclableByteBuffer();
            int num = GetBasicsCore(buffer.Buffer, buffer.Buffer.Length, contentInfo);
            while (num < 0)
            {
                buffer.Resize(-num);
                num = GetBasicsCore(buffer.Buffer, buffer.Buffer.Length, contentInfo);
            }
            if (num == 0)
                throw new HttpException("Cannot_retrieve_request_data");
            string[] strArray = buffer.GetDecodedTabSeparatedStrings(Encoding.Default, 6, 0);
            buffer.Dispose();
            return strArray;
        }

        public override int ReadEntityBody(byte[] buffer, int size)
        {
            return ReadEntityBody(buffer, 0, size);
        }

        public override int ReadEntityBody(byte[] buffer, int offset, int size)
        {
            if ((buffer.Length - offset) < size)
                throw new ArgumentOutOfRangeException("offset");
            int num = GetAdditionalPostedContentCore(buffer, offset, size);
            if (num < 0)
                throw new HttpException("Cannot_read_posted_data");
            return num;
        }

        internal virtual void ReadRequestBasics()
        {
            var contentInfo = new int[4];
            var strArray = ReadBasics(contentInfo);
            if ((strArray == null) || (strArray.Length != 6))
                throw new HttpException("Cannot_retrieve_request_data");
            _contentType = contentInfo[0];
            _contentTotalLength = contentInfo[1];
            _contentAvailLength = contentInfo[2];
            _queryStringLength = contentInfo[3];
            _method = strArray[0];
            _filePath = strArray[1];
            _pathInfo = strArray[2];
            _path = (_pathInfo.Length > 0 ? _filePath + _pathInfo : _filePath);
            _pathTranslated = strArray[3];
            _appPath = strArray[4];
            _appPathTranslated = strArray[5];
        }

        private void ReadRequestHeaders()
        {
            if (!_requestHeadersAvailable)
            {
                _knownRequestHeaders = new string[40];
                var list = new ArrayList();
                string serverVariable = GetServerVariable("ALL_RAW");
                int num = (serverVariable != null ? serverVariable.Length : 0);
                int startIndex = 0;
                while (startIndex < num)
                {
                    int num3 = serverVariable.IndexOfAny(_colonOrNL, startIndex);
                    if (num3 < 0)
                        break;
                    if (serverVariable[num3] == '\n')
                        startIndex = num3 + 1;
                    else
                    {
                        if (num3 == startIndex)
                        {
                            startIndex++;
                            continue;
                        }
                        var header = serverVariable.Substring(startIndex, num3 - startIndex).Trim();
                        int index = serverVariable.IndexOf('\n', num3 + 1);
                        if (index < 0)
                            index = num;
                        while ((index < (num - 1)) && (serverVariable[index + 1] == ' '))
                        {
                            index = serverVariable.IndexOf('\n', index + 1);
                            if (index < 0)
                                index = num;
                        }
                        string str3 = serverVariable.Substring(num3 + 1, (index - num3) - 1).Trim();
                        int knownRequestHeaderIndex = HttpWorkerRequest.GetKnownRequestHeaderIndex(header);
                        if (knownRequestHeaderIndex >= 0)
                            _knownRequestHeaders[knownRequestHeaderIndex] = str3;
                        else
                        {
                            list.Add(header);
                            list.Add(str3);
                        }
                        startIndex = index + 1;
                    }
                }
                int num6 = list.Count / 2;
                _unknownRequestHeaders = new string[num6][];
                int num7 = 0;
                for (startIndex = 0; startIndex < num6; startIndex++)
                    _unknownRequestHeaders[startIndex] = new string[] { (string)list[num7++], (string)list[num7++] };
                _requestHeadersAvailable = true;
            }
        }
    }
}
